
/*scripts/login.js 编码为utf-8 */
$(function(){
//	console.log("waitum !!");
	$('#login').click(loginAction);
	$('#count').blur(checkName);
	$('#password').blur(checkPassword);
	$('#regist_button').click(registAction);
	$('#regist_username').blur(checkRegistName);
	$('#regist_password').blur(checkRegistPassword);
	$('#final_password').blur(checkRegistConfirm);
	
	
});


function checkRegistConfirm(){
	var pwd=$('#regist_password').val();
	var pwd2=$('#final_password').val();
	//pwd为空值表示false,否则为true
	if(pwd && pwd==pwd2){
		$('#final_password').next().hide();
		return true;
	}
	$('#final_password').next().show().find('span').html('确认密码不一致');
	return false;
}




function checkRegistPassword(){
	var pwd=$('#regist_password').val().trim();
	var rule=/^\w{6,16}$/;
	if(rule.test(pwd)){
		$('#regist_password').next().hide();
		return true;
	}
	$('#regist_password').next().show().find('span').html('6-16个字符串')
	return false;
	
}



function checkRegistName(){
	
	var rule=/^\w{4,10}$/;
	var registName=$('#regist_username').val();
	if(rule.test(registName)){
		$('#regist_username').next().hide();
		return true;
	}
	$('#regist_username').next().show().find('span').html('4-10个字符串');
	return false;
	
}


function registAction(){
	//检验界面参数
	
	var n=checkRegistName()+checkRegistPassword()+checkRegistConfirm();
	if(n!=3){
		return;
	}
	
	//获取表单数据
	var name=$('#regist_username').val().trim();
	var password=$('#regist_password').val();
	var confirm=$('#final_password').val();
	var nick=$('#nickname').val();
	
	//发起ajax请求
	var url='user/regist.do';
	var data={'name':name,
			'nick':nick,
			'password':password,
			'confirm':confirm};
	
	$.post(url,data,
		function(result){
			console.log(result);
			if(result.state==0){
				//退回到登录页面
				$('#back').click();
				var name=result.data.name;
				$('#count').val(name);
				$('#password').focus();
				
			}else if(result.state==4){
				$('#regist_username').next().show().find('span').html(result.message);
			}else if(result.state==3){
				$('#regist_password').next().show().find('span').html(result.message);
			}else{
				
				alert(result.message);
			
			}
		
		});
	
	
	
	
	//得到响应参数后,更新页面
	
	
	
	
}




function checkPassword(){
	var name=$('#password').val();
	var rule=/^\w{6,16}$/;
	if(!rule.test(name)){
		$('#password').next().html('6-16个字符串');
		return false;
	}
	$('#password').next().empty();
	return true;
	
}
function checkName(){
	var name=$('#count').val();
	var rule=/^\w{4,10}$/;
	if(!rule.test(name)){
		$('#count').next().html('4-10个字符串');
		return false;
	}
	$('#count').next().empty();
	return true;
	
}

function loginAction(){
//	console.log("loginAction");
	//获取用户名和密码
	var name=$('#count').val();
	var password=$('#password').val();
	//data对象中的属性名要与服务器的控制器的参数名一致!
	var n=checkName()+checkPassword();
	
	if(n!=2){
		return;
	}

	var datas = {"name":name , "password":password};
	$.ajax({
		url:"user/login.do",
		data:datas,
		type:'post',
		success:function(result){
			if(result.state==0){
				//登录成功
				var user=result.data;
				console.log(user);
				//登录成功后将userId保存到cookie中
				addCookie('userId',user.id);
				
				
				//登录成功,跳转到edit.html
				location.href="edit.html";
				
				
			}else{
				var msg=result.message;
				if(result.state==2){
					$('#count').next().html(msg);
					
				}else if(result.state==3){
					$('#password').next().html(msg);
				}else{
					alert(msg);
				}
				
				
				
			}
			
		},
			
		error:function(){
			alert('通讯故障');
		}
			
	});
			
}
	
	
	
	
	
	



















